package com.ccp.dev.demo.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.demo.model.WDemozb;
import com.ccp.dev.demo.service.WDemozbService;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.core.basic.annotion.Action;
import java.util.List;
import java.util.Map;

import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@Controller
@Action(ownermodel = SysAuditModelType.NULL)
//格式：项目名/模块名
@RequestMapping("/demo/wDemozb")
public class WDemozbController extends SysBaseController{

	@Resource
	private WDemozbService wDemozbService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: cai
     * @date: 2020-05-25 10:27:45
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="跳转到demo主首页", exectype = "页面跳转日志")
    public String viewList() {
        return "wDemozb/list";
    }

    /**
    * 功能描述: 附带检索条件的分页查询
    *
    * @param: [request] http请求
    * @return: java.lang.Object
    * @auther: cai
    * @date: 2020-05-25 10:27:45
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询demo主",detail="查询数据信息<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        //分页数据
        List<WDemozb> pageDatas = wDemozbService.queryList(queryFilter);
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }


    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: cai
     * @date: 2020-05-25 10:27:45
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="跳转到demo主新增页面", exectype = "页面跳转日志")
    public String viewAdd(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        return "wDemozb/add";
    }

    /**
    * 功能描述:  数据保存通用方法
    *
    * @param: wDemozb 保存的对象
    * @return: java.lang.Object
    * @auther: cai
    * @date: 2020-05-25 10:27:45
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存demo数据",detail="<#if isAdd>新增<#else>编辑</#if>demo主数据<#if success>成功<#else>失败</#if>")
    public Object save(HttpServletRequest request) {
        String data = RequestUtil.getString(request, "formData");
        data = StringEscapeUtils.unescapeHtml4(data);
        String mainTableId = RequestUtil.getString(request, "mainTableId");
        int result;
        if (StringUtil.isEmpty(mainTableId)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd",true);
            //新增操作
            result = wDemozbService.addDemo(data);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            //修改操作
            result = wDemozbService.updateDemo(mainTableId,data);
        }
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }

    /**
    * 功能描述: 跳转到详情页面
    *
    * @return: java.lang.String
    * @auther: cai
    * @date: 2020-05-25 10:27:45
    */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/particulars")
    @Action(description="跳转到demo主详情页面", exectype = "页面跳转日志")
    public String viewParticulars(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        return "wDemozb/particulars";
    }

    /**
    * 功能描述: 根据主键id获取一条数据的通用查询方法
    *
    * @param: id 参数对象必须是"id"
    * @return: java.lang.Object
    * @auther: cai
    * @date: 2020-05-25 10:27:45
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询demo主的一条数据",detail="根据主键id查询demo主的数据<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("mainTableId") String mainTableId) {
        Map formData = wDemozbService.getMainSubFormByMainId1(mainTableId);
        if (formData.isEmpty()) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return getResultDataSuccess("get.success",formData);
    }

    /**
    * 功能描述: 根据主键ID删除数据的通用删除方法
    *
    * @param: id 参数名称必须是"id"
    * @return: java.lang.Object
    * @auther: cai
    * @date: 2020-05-25 10:27:45
    */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除demo主数据",exectype = "删除日志",detail="删除数据信息<#if success>成功<#else>失败</#if>")
    public Object delete(@RequestParam("id") String id) {
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return fail("delete.failure.message");
        }
        int result = wDemozbService.delByIdArray(id.split(","));
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }

}
